package com.xuxin.baby.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.contract.ChildDataRecordContract;
import com.xuxin.baby.presenter.ChildDataRecordPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.DatePickerUtils;
import com.zjun.widget.RuleView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.08.12 0012
 * 记身高
 */
public class ChildDataRecordActivity extends BaseActivity implements ChildDataRecordContract.View {
    private final ChildDataRecordContract.Presenter presenter = new ChildDataRecordPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.child_data_record_time)
    TextView child_data_record_time;
    @BindView(R.id.child_data_record_height)
    TextView child_data_record_height;
    @BindView(R.id.child_data_record_weight)
    TextView child_data_record_weight;
    @BindView(R.id.child_data_record_head)
    TextView child_data_record_head;
    @BindView(R.id.child_data_record_height_rule)
    RuleView child_data_record_height_rule;
    @BindView(R.id.child_data_record_weight_rule)
    RuleView child_data_record_weight_rule;
    @BindView(R.id.child_data_record_head_rule)
    RuleView child_data_record_head_rule;

    private String tenantId;//租户id
    private String token;//授权token
    private String childId;//宝宝ID
    private float height;//身高
    private float weight;//体重
    private float headCircle;//头围
    private String time;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_child_data_record);
    }

    @Override
    public void initView() {
        title_name.setText("记身高");
        button.setText("保存");
        child_data_record_time.setText(TimeUtils.getNowString(TimeUtils.getSafeDateFormat("yyyy-MM-dd")));
        child_data_record_height_rule.setOnValueChangedListener(new RuleView.OnValueChangedListener() {
            @Override
            public void onValueChanged(float value) {
                child_data_record_height.setText(String.valueOf(value));
            }
        });
        child_data_record_weight_rule.setOnValueChangedListener(new RuleView.OnValueChangedListener() {
            @Override
            public void onValueChanged(float value) {
                child_data_record_weight.setText(String.valueOf(value));
            }
        });
        child_data_record_head_rule.setOnValueChangedListener(new RuleView.OnValueChangedListener() {
            @Override
            public void onValueChanged(float value) {
                child_data_record_head.setText(String.valueOf(value));
            }
        });
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(this);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        childId = aCache.getAsString(BaseConstant.childId);
    }

    @Override
    public void onComplete() {
        dismiss();
    }

    @Override
    @OnClick({R.id.title_left, R.id.child_data_record_time, R.id.button})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.child_data_record_time) {
            DatePickerUtils.showDate(context, new DatePickerUtils.onListener() {
                @Override
                public void onShow(String time) {
                    child_data_record_time.setText(time);
                }
            });
        } else if (id == R.id.button) {
            try {
                height = child_data_record_height_rule.getCurrentValue();
                weight = child_data_record_weight_rule.getCurrentValue();
                headCircle = child_data_record_head_rule.getCurrentValue();
                time = child_data_record_time.getText().toString();
                popup.show();
                handler.sendEmptyMessage(0);
            } catch (Exception e) {
                e.printStackTrace();
                dismiss();
            }
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {//记身高
                presenter.record(tenantId, token, time, childId, height, weight, headCircle, 0, 0, "basic");
            }
            return false;
        }
    });

    @Override
    public void onSuccess(BaseBean baseBean) {
        setResult(200);
        finish();
    }
}